#!/usr/bin/make -f
# This rules file is usable only for PowerDNS.COM static builds.
# If you want a Debian-downstream-style package, please use the packaging from Debian.

tmpdir		:= $(shell pwd)/debian/tmp
tools_tmpdir	:= $(shell pwd)/debian/tmp-tools

build-static stamp-build-static:
	./configure \
		CXX=$(shell pwd)/build-scripts/semistaticg++ \
		PKG_CONFIG_PATH=/opt/botan-1.10/lib/pkgconfig/ \
		--prefix=/usr \
		--libexecdir='$${prefix}/lib' \
		--libdir='$${prefix}/lib' \
		--sysconfdir=/etc/powerdns \
		--infodir='$${datadir}/info' \
		--mandir='$${datadir}/man' \
		--with-pgsql-lib=/opt/postgresql/lib --with-pgsql-includes=/opt/postgresql/include \
		--with-modules="bind gmysql gpgsql pipe gsqlite3 lua geoip tinydns mydns opendbx remote random" \
		--with-dynmodules="" \
		--enable-botan1.10 \
		--enable-static-binaries \
		--enable-tools
	make -j4
	touch stamp-build-static


binary-main-prepare: 
	rm -f debian/substvars
	rm -rf "$(tmpdir)" "$(tmpdir)"-*
	rm -rf "$(tools_tmpdir)" "$(tools_tmpdir)"-*
	install -d -m 755 -o root -g root "$(tmpdir)"
	install -d -m 755 -o root -g root "$(tools_tmpdir)"
	make DESTDIR="$(tmpdir)" install

	rm -f "$(tmpdir)"/usr/bin/binpatch
	rm -f "$(tmpdir)"/usr/bin/zone2ldap
	rm -rf "$(tmpdir)"/usr/lib
	rm -f "$(tmpdir)"/usr/share/man/man1/zone2ldap.1
	strip --remove-section=.comment --remove-section=.note \
		--strip-unneeded \
		"$(tmpdir)"/usr/bin/zone2sql \
		"$(tmpdir)"/usr/sbin/pdns_server \
		"$(tmpdir)"/usr/bin/pdns_control
	mv "$(tmpdir)"/etc/powerdns/pdns.conf-dist "$(tmpdir)"/etc/powerdns/pdns.conf

	install -d -m 755 -o root -g root "$(tmpdir)"/etc/init.d
	install -p -m 755 -o root -g root pdns/pdns.init \
		"$(tmpdir)"/etc/init.d/pdns

	install -d -m 755 -o root -g root \
		"$(tmpdir)"/usr/share/doc/pdns
	install -p -m 644 -o root -g root debian/changelog \
			"$(tmpdir)"/usr/share/doc/pdns/changelog.Debian
	gzip -9 "$(tmpdir)"/usr/share/doc/pdns/c*
	install -p -m 644 -o root -g root debian/copyright \
			"$(tmpdir)"/usr/share/doc/pdns/
	
	install -d -m 755 -o root -g root "$(tmpdir)"/DEBIAN
	install -p -m 755 -o root -g root debian/pdns.prerm \
			"$(tmpdir)"/DEBIAN/prerm
	install -p -m 755 -o root -g root debian/pdns.postrm \
			"$(tmpdir)"/DEBIAN/postrm
	install -p -m 755 -o root -g root debian/pdns.postinst \
			"$(tmpdir)"/DEBIAN/postinst
	install -p -m 644 -o root -g root debian/pdns.conffiles \
			"$(tmpdir)"/DEBIAN/conffiles


	install -d -m 755 -o root -g root \
		"$(tools_tmpdir)"/usr/bin
	install -d -m 755 -o root -g root \
		"$(tools_tmpdir)"/usr/share/man/man8
	install -d -m 755 -o root -g root \
		"$(tools_tmpdir)"/usr/share/man/man1
	install -d -m 755 -o root -g root \
		"$(tools_tmpdir)"/usr/share/doc/pdns-tools
	install -p -m 644 -o root -g root debian/changelog \
			"$(tools_tmpdir)"/usr/share/doc/pdns-tools/changelog.Debian
	gzip -9 "$(tools_tmpdir)"/usr/share/doc/pdns-tools/c*
	install -p -m 644 -o root -g root debian/copyright \
			"$(tools_tmpdir)"/usr/share/doc/pdns-tools/

	install -d -m 755 -o root -g root "$(tools_tmpdir)"/DEBIAN

	#FIXME: shell loops hide errors
	for prog in dnsbulktest dnsgram dnsreplay dnsscan dnsscope dnstcpbench dnswasher \
		notify nproxy nsec3dig saxfr ; do \
		mv "$(tmpdir)"/usr/bin/$$prog "$(tools_tmpdir)"/usr/bin ; \
		[ -e "$(tmpdir)"/usr/share/man/man8/"$$prog".8 ] && \
			mv "$(tmpdir)"/usr/share/man/man8/"$$prog".8 "$(tools_tmpdir)"/usr/share/man/man8/ ; \
		[ -e "$(tmpdir)"/usr/share/man/man1/"$$prog".1 ] && \
			mv "$(tmpdir)"/usr/share/man/man1/"$$prog".1 "$(tools_tmpdir)"/usr/share/man/man1/ ; \
		strip --remove-section=.comment --remove-section=.note \
			--strip-unneeded \
			"$(tools_tmpdir)"/usr/bin/"$$prog" ; \
	done


binary-static: stamp-build-static binary-main-prepare
	dpkg-gencontrol -fdebian/files -cdebian/control -ldebian/changelog  -isp -ppdns-static -P"$(tmpdir)"
	dpkg-gencontrol -fdebian/files -cdebian/control -ldebian/changelog  -isp -ppdns-tools -P"$(tools_tmpdir)"
	dpkg --build "$(tmpdir)" ..
	dpkg --build "$(tools_tmpdir)" ..


clean:
	-make distclean
	-make -C docs clean
	rm -f debian/files debian/substvars stamp-build stamp-build-static
	rm -rf "$(tmpdir)"

.PHONY: clean

